{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "473622a9",
   "metadata": {},
   "source": [
    "### Part I: Constructing a Domesday Book for 2025 with AI to Enhance Summaries of Place"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4b43cf9e",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import geopandas as gpd\n",
    "import seaborn as sns\n",
    "import matplotlib.pyplot as plt\n",
    "import folium\n",
    "import folium.plugins as fp"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "205bc044",
   "metadata": {},
   "outputs": [],
   "source": [
    "bua_2022_gdf = gpd.read_file(\"C:/Users/georg/Documents/work/2508_domesday_python_ai/BUA_2022_GB.gpkg\")\n",
    "bua_2022_gdf['region'] = np.select([bua_2022_gdf['BUA22CD'].str[0] == \"E\", bua_2022_gdf['BUA22CD'].str[0] == \"W\", bua_2022_gdf['BUA22CD'].str[0] == \"S\"], [\"England\", \"Wales\", \"Scotland\"], default=\"England\")\n",
    "bua_2022_gdf = bua_2022_gdf[bua_2022_gdf['region'].isin([\"England\", \"Wales\"])].reset_index(drop=True)\n",
    "bua_2022_gdf['area_h'] = bua_2022_gdf['geometry'].area\n",
    "bua_2022_gdf = bua_2022_gdf.to_crs(\"EPSG:4326\")\n",
    "bua_2022_gdf['area_h_percentile'] = bua_2022_gdf['area_h'].rank(pct=True)\n",
    "# bua_2022_gdf_large = bua_2022_gdf[bua_2022_gdf['area_h_percentile'] < 0.001].reset_index(drop=True)\n",
    "bua_2022_gdf['word_limit'] = (20 + round((bua_2022_gdf['area_h_percentile'] * 140),0))\n",
    "bua_2022_gdf_regional = bua_2022_gdf #[bua_2022_gdf['LAT'] > 55.00].reset_index()\n",
    "print(bua_2022_gdf_regional.info())\n",
    "sns.displot(data=bua_2022_gdf_regional['word_limit'])\n",
    "plt.show()\n",
    "# bua_2022_gdf_regional.explore('region')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e595964c",
   "metadata": {},
   "outputs": [],
   "source": [
    "bua_2022_gdf_regional_explore = bua_2022_gdf_regional[['BUA22NM', 'geometry']]\n",
    "bua_2022_gdf_regional_explore.columns = [\"PLACE\", \"geometry\"]\n",
    "bua_2022_gdf_regional_explore"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6b9b91eb",
   "metadata": {},
   "outputs": [],
   "source": [
    "# GENERATE INTERACTIVE OUTPUT MAP\n",
    "\n",
    "map = folium.Map([53.00, -2.00], tiles=None, zoom_start=8)\n",
    "\n",
    "tiles_osm_fg = folium.FeatureGroup(name=\"Basemap: OSM Standard\", show=True, control=True)\n",
    "folium.TileLayer(tiles=\"OpenStreetMap\", show=True, control=False).add_to(tiles_osm_fg)\n",
    "map.add_child(tiles_osm_fg)\n",
    "places_fg = folium.FeatureGroup(name=\"SETTLEMENTS OF BRITAIN\", show=True, control=False)\n",
    "\n",
    "search_fg = folium.FeatureGroup(name=\"Search\", show=False, control=False)\n",
    "search_layer = folium.GeoJson(bua_2022_gdf_regional_explore, style_function=lambda x: {'opacity': 0.00, 'fillOpacity': 0.00}).add_to(search_fg)\n",
    "map.add_child(search_fg)\n",
    "folium.plugins.Search(layer=search_fg, geom_type='Polygon', search_label='PLACE', collapsed=True).add_to(map)\n",
    "\n",
    "for _, row_i in bua_2022_gdf_regional_explore.iterrows():\n",
    "    json = gpd.GeoSeries(row_i['geometry']).to_json()\n",
    "    geo_json = folium.GeoJson(json, \n",
    "                              style_function=lambda x: {'fillColor': 'black', 'fillOpacity': 0.56, 'color': 'black', 'weight': 0.6, 'dashArray': \"2, 2\"},\n",
    "                              highlight_function=lambda x: {\"fillColor\": 'green'})\n",
    "    geo_json.add_to(places_fg)\n",
    "map.add_child(places_fg)\n",
    "\n",
    "fp.Fullscreen(title=\"Enter Full Screen\", force_separate_button=True).add_to(map)\n",
    "fp.MiniMap(tile_layer=\"OpenStreetMap\", toggle_display=True, zoom_animation=True, zoom_level_fixed=5, position=\"bottomright\").add_to(map)\n",
    "\n",
    "# map\n",
    "\n",
    "map.save(\"2510_britain_places_output_map.html\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
